package tech.abing.spider.task.pageprocessor.amac;

import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.OutsourceOrganizationEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class OutsourceOrganizationDetailProcess implements PageProcessor{

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		
		OutsourceOrganizationEntity outOrg = new OutsourceOrganizationEntity();
		Element table = doc.select("#body > form > table").first();
		
		outOrg.setOrgName(table.getElementsMatchingOwnText("机构名称").first().parent().nextElementSibling().text());
		outOrg.setOrgCode((table.getElementsMatchingOwnText("机构代码").first().parent().nextElementSibling().text()));
		outOrg.setRegisterCode(table.getElementsMatchingOwnText("备案编号").first().parent().nextElementSibling().text());
		outOrg.setOrgType(table.getElementsMatchingOwnText("机构类型").first().parent().nextElementSibling().text());
		outOrg.setOrgUrl(table.getElementsMatchingOwnText("公司网址").first().parent().nextElementSibling().text());
		outOrg.setRegisterAddress(table.getElementsMatchingOwnText("注册地址").first().parent().nextElementSibling().text());
		outOrg.setOfficeAddress(table.getElementsMatchingOwnText("办公地址").first().parent().nextElementSibling().text());
		outOrg.setArtificialPersonName(table.getElementsMatchingOwnText("法定代表人/执行事务合伙人").first().parent().nextElementSibling().text());
		outOrg.setServiceType(table.getElementsMatchingOwnText("服务类型").first().parent().nextElementSibling().text());
		outOrg.setServiceRegTime(DateUtils.parseDate(table.getElementsMatchingOwnText("本服务备案时间").first().parent().nextElementSibling().text()));
		outOrg.setStaffNumber(Integer.valueOf(StringUtils.removeSpace(table.getElementsMatchingOwnText("服务部门/团队人员数量").first().parent().nextElementSibling().text())));
		outOrg.setFundStaffNumber(Integer.valueOf(StringUtils.removeSpace(table.getElementsMatchingOwnText("含基金从业人员数量").first().parent().nextElementSibling().text())));
		outOrg.setBackup(table.getElementsMatchingOwnText("备注").first().parent().nextElementSibling().text());
		outOrg.setUrl(page.getRequest().getUrl());
		outOrg.setId(UuIdPublic.timeStrongID(5));
		StringUtils.removeSpace(outOrg);
		objectContainer.add(outOrg);
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		
		
	}

}
